//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.UI.UserControls
{
	public abstract class UrlControl : Framework.UserControlBase
	{
		protected System.Web.UI.HtmlControls.HtmlTableRow TypeRow;
		protected System.Web.UI.WebControls.RadioButtonList optType;
		protected System.Web.UI.HtmlControls.HtmlTableRow URLRow;
		protected System.Web.UI.WebControls.Label lblURL;
		protected System.Web.UI.WebControls.DropDownList cboUrls;
		protected System.Web.UI.WebControls.TextBox txtUrl;
		protected System.Web.UI.WebControls.LinkButton cmdSelect;
		protected System.Web.UI.WebControls.LinkButton cmdAdd;
		protected System.Web.UI.WebControls.LinkButton cmdDelete;
		protected System.Web.UI.WebControls.Label lblURLType;
		protected System.Web.UI.HtmlControls.HtmlTableRow ImagesRow;
		protected System.Web.UI.WebControls.Label lblImages;
		protected System.Web.UI.WebControls.DropDownList cboImages;
		protected System.Web.UI.HtmlControls.HtmlTableRow TabRow;
		protected System.Web.UI.WebControls.Label lblTab;
		protected System.Web.UI.WebControls.DropDownList cboTabs;
		protected System.Web.UI.HtmlControls.HtmlTableRow FileRow;
		protected System.Web.UI.WebControls.Label lblFolder;
		protected System.Web.UI.WebControls.DropDownList cboFolders;
		protected System.Web.UI.WebControls.Label lblFile;
		protected System.Web.UI.WebControls.DropDownList cboFiles;
		protected System.Web.UI.HtmlControls.HtmlInputFile txtFile;
		protected System.Web.UI.WebControls.LinkButton cmdUpload;
		protected System.Web.UI.WebControls.LinkButton cmdSave;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.Image imgStorageLocationType;
		protected System.Web.UI.HtmlControls.HtmlTableRow UserRow;
		protected System.Web.UI.WebControls.Label lblUser;
		protected System.Web.UI.WebControls.TextBox txtUser;
		protected System.Web.UI.HtmlControls.HtmlTableRow ErrorRow;
		protected System.Web.UI.WebControls.Label lblMessage;
		protected System.Web.UI.WebControls.CheckBox chkNewWindow;
		protected System.Web.UI.WebControls.CheckBox chkTrack;
		protected System.Web.UI.WebControls.CheckBox chkLog;
		private string _localResourceFile;
		private PortalInfo _objPortal;
		private bool _doChangeURL = false;
		private bool _doRenderTypes = false;
		private bool _doRenderTypeControls = false;
		private bool _doRenderTrackingOptions = false;
		private bool _doReloadFolders = false;
		private bool _doReloadFiles = false;
		public string FileFilter
		{
			get
			{
				if (ViewState["FileFilter"] != null)
				{
					return Convert.ToString(ViewState["FileFilter"]);
				}
				else
				{
					return "";
				}
			}
			set
			{
				ViewState["FileFilter"] = value;
				if (IsTrackingViewState)
				{
					_doReloadFiles = true;
				}
			}
		}
		public bool IncludeActiveTab
		{
			get
			{
				if (ViewState["IncludeActiveTab"] != null)
				{
					return Convert.ToBoolean(ViewState["IncludeActiveTab"]);
				}
				else
				{
					return false;
				}
			}
			set
			{
				ViewState["IncludeActiveTab"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypeControls = true;
				}
			}
		}
		public string LocalResourceFile
		{
			get
			{
				string fileRoot;
				if (String.IsNullOrEmpty(_localResourceFile))
				{
					fileRoot = this.TemplateSourceDirectory + "/" + Localization.LocalResourceDirectory + "/URLControl.ascx";
				}
				else
				{
					fileRoot = _localResourceFile;
				}
				return fileRoot;
			}
			set { _localResourceFile = value; }
		}
		public bool Log
		{
			get
			{
				if (chkLog.Visible == true)
				{
					return chkLog.Checked;
				}
				else
				{
					return false;
				}
			}
		}
		public int ModuleID
		{
			get
			{
				int myMid = -2;
				if (ViewState["ModuleId"] != null)
				{
					myMid = Convert.ToInt32(ViewState["ModuleId"]);
				}
				else if (Request.QueryString["mid"] != null)
				{
					Int32.TryParse(Request.QueryString["mid"], out myMid);
				}
				return myMid;
			}
			set { ViewState["ModuleId"] = value; }
		}
		public bool NewWindow
		{
			get
			{
				if (chkNewWindow.Visible == true)
				{
					return chkNewWindow.Checked;
				}
				else
				{
					return false;
				}
			}
		}
		public bool Required
		{
			get
			{
				if (ViewState["Required"] != null)
				{
					return Convert.ToBoolean(ViewState["Required"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["Required"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypeControls = true;
				}
			}
		}
		public bool ShowFiles
		{
			get
			{
				if (ViewState["ShowFiles"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowFiles"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["ShowFiles"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypes = true;
				}
			}
		}
		public bool ShowImages
		{
			get
			{
				if (ViewState["ShowImages"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowImages"]);
				}
				else
				{
					return false;
				}
			}
			set
			{
				ViewState["ShowImages"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypes = true;
				}
			}
		}
		public bool ShowLog
		{
			get { return chkLog.Visible; }
			set { chkLog.Visible = value; }
		}
		public bool ShowNewWindow
		{
			get { return chkNewWindow.Visible; }
			set { chkNewWindow.Visible = value; }
		}
		public bool ShowNone
		{
			get
			{
				if (ViewState["ShowNone"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowNone"]);
				}
				else
				{
					return false;
				}
			}
			set
			{
				ViewState["ShowNone"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypes = true;
				}
			}
		}
		public bool ShowSecure
		{
			get
			{
				if (ViewState["ShowSecure"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowSecure"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["ShowSecure"] = value;
				if (IsTrackingViewState)
				{
					_doReloadFolders = true;
				}
			}
		}
		public bool ShowDatabase
		{
			get
			{
				if (ViewState["ShowDatabase"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowDatabase"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["ShowDatabase"] = value;
				if (IsTrackingViewState)
				{
					_doReloadFolders = true;
				}
			}
		}
		public bool ShowTabs
		{
			get
			{
				if (ViewState["ShowTabs"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowTabs"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["ShowTabs"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypes = true;
				}
			}
		}
		public bool ShowTrack
		{
			get { return chkTrack.Visible; }
			set { chkTrack.Visible = value; }
		}
		public bool ShowUpLoad
		{
			get
			{
				if (ViewState["ShowUpLoad"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowUpLoad"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["ShowUpLoad"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypeControls = true;
				}
			}
		}
		public bool ShowUrls
		{
			get
			{
				if (ViewState["ShowUrls"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowUrls"]);
				}
				else
				{
					return true;
				}
			}
			set
			{
				ViewState["ShowUrls"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypes = true;
				}
			}
		}
		public bool ShowUsers
		{
			get
			{
				if (ViewState["ShowUsers"] != null)
				{
					return Convert.ToBoolean(ViewState["ShowUsers"]);
				}
				else
				{
					return false;
				}
			}
			set
			{
				ViewState["ShowUsers"] = value;
				if (IsTrackingViewState)
				{
					_doRenderTypes = true;
				}
			}
		}
		public bool Track
		{
			get
			{
				if (chkTrack.Visible == true)
				{
					return chkTrack.Checked;
				}
				else
				{
					return false;
				}
			}
		}
		public string Url
		{
			get
			{
				string r = "";
				string strCurrentType = "";
				if (optType.Items.Count > 0 && optType.SelectedIndex >= 0)
				{
					strCurrentType = optType.SelectedItem.Value;
				}
				switch (strCurrentType)
				{
					case "I":
						if (cboImages.SelectedItem != null)
						{
							r = cboImages.SelectedItem.Value;
						}
						break;
					case "U":
						if (cboUrls.Visible)
						{
							if (cboUrls.SelectedItem != null)
							{
								r = cboUrls.SelectedItem.Value;
								txtUrl.Text = r;
							}
						}
						else
						{
							string mCustomUrl = txtUrl.Text;
							if (mCustomUrl.ToLower() == "http://")
							{
								r = "";
							}
							else
							{
								r = Common.Globals.AddHTTP(mCustomUrl);
							}
						}
						break;
					case "T":
						string strTab = "";
						if (cboTabs.SelectedItem != null)
						{
							strTab = cboTabs.SelectedItem.Value;
							if (Regex.IsMatch(strTab, "^\\d+$") && (Convert.ToInt32(strTab) >= 0))
							{
								r = strTab;
							}
						}
						break;
					case "F":
						if (cboFiles.SelectedItem != null)
						{
							if (!String.IsNullOrEmpty(cboFiles.SelectedItem.Value))
							{
								r = "FileID=" + cboFiles.SelectedItem.Value;
							}
							else
							{
								r = "";
							}
						}
						break;
					case "M":
						if (!String.IsNullOrEmpty(txtUser.Text))
						{
							UserInfo objUser = UserController.GetCachedUser(_objPortal.PortalID, txtUser.Text);
							if (objUser != null)
							{
								r = "UserID=" + objUser.UserID.ToString();
							}
							else
							{
								lblMessage.Text = Localization.GetString("NoUser", this.LocalResourceFile);
								txtUser.Text = "";
							}
						}
						break;
				}
				return r;
			}
			set
			{
				ViewState["Url"] = value;
				if (IsTrackingViewState)
				{
					_doChangeURL = true;
					_doReloadFiles = true;
				}
			}
		}
		public string UrlType
		{
			get { return Convert.ToString(ViewState["UrlType"]); }
			set
			{
				if (value != null && !String.IsNullOrEmpty(value.Trim()))
				{
					ViewState["UrlType"] = value;
					if (IsTrackingViewState)
					{
						_doChangeURL = true;
					}
				}
			}
		}
		public string Width
		{
			get { return Convert.ToString(ViewState["SkinControlWidth"]); }
			set
			{
				if (!String.IsNullOrEmpty(value))
				{
					cboUrls.Width = System.Web.UI.WebControls.Unit.Parse(value);
					txtUrl.Width = System.Web.UI.WebControls.Unit.Parse(value);
					cboImages.Width = System.Web.UI.WebControls.Unit.Parse(value);
					cboTabs.Width = System.Web.UI.WebControls.Unit.Parse(value);
					cboFolders.Width = System.Web.UI.WebControls.Unit.Parse(value);
					cboFiles.Width = System.Web.UI.WebControls.Unit.Parse(value);
					txtUser.Width = System.Web.UI.WebControls.Unit.Parse(value);
					ViewState["SkinControlWidth"] = value;
				}
			}
		}
		private ArrayList GetFileList(bool NoneSpecified)
		{
			int PortalId = Null.NullInteger;
			if ((!IsHostMenu) || (Request.QueryString["pid"] != null))
			{
				PortalId = _objPortal.PortalID;
			}
			return Common.Globals.GetFileList(PortalId, FileFilter, NoneSpecified, cboFolders.SelectedItem.Value, false);
		}
		private void LoadFolders(string Permissions)
		{
			int PortalId = Null.NullInteger;
			cboFolders.Items.Clear();
			if ((!IsHostMenu) || (Request.QueryString["pid"] != null))
			{
				PortalId = _objPortal.PortalID;
			}
			ArrayList folders = FileSystemUtils.GetFoldersByUser(PortalId, ShowSecure, ShowDatabase, Permissions);
			foreach (FolderInfo folder in folders)
			{
				ListItem FolderItem = new ListItem();
				if (folder.FolderPath == Null.NullString)
				{
					FolderItem.Text = Localization.GetString("Root", this.LocalResourceFile);
				}
				else
				{
                    FolderItem.Text = folder.DisplayPath;
				}
				FolderItem.Value = folder.FolderPath;
				cboFolders.Items.Add(FolderItem);
			}
		}
		private void LoadUrls()
		{
			UrlController objUrls = new UrlController();
			cboUrls.Items.Clear();
			cboUrls.DataSource = objUrls.GetUrls(_objPortal.PortalID);
			cboUrls.DataBind();
		}
		private void SetStorageLocationType()
		{
			FolderController objFolder = new FolderController();
			FolderInfo objFolderInfo = new FolderInfo();
			string FolderName = cboFolders.SelectedValue;
			if (cboFolders.SelectedValue == string.Empty)
			{
				FolderName = "";
			}
			objFolderInfo = objFolder.GetFolder(PortalSettings.PortalId, FolderName, false);
			if (objFolderInfo != null)
			{
				switch (objFolderInfo.StorageLocation)
				{
					case (int)FolderController.StorageLocationTypes.InsecureFileSystem:
						imgStorageLocationType.Visible = false;
						break;
					case (int)FolderController.StorageLocationTypes.SecureFileSystem:
						imgStorageLocationType.ImageUrl = ResolveUrl("~/images/icon_securityroles_16px.gif");
						imgStorageLocationType.Visible = true;
						break;
					case (int)FolderController.StorageLocationTypes.DatabaseSecure:
						imgStorageLocationType.ImageUrl = ResolveUrl("~/images/icon_sql_16px.gif");
						imgStorageLocationType.Visible = true;
						break;
				}
			}
		}
		private void DoChangeURL()
		{
			string _Url = Convert.ToString(ViewState["Url"]);
			string _Urltype = Convert.ToString(ViewState["UrlType"]);
			if (!String.IsNullOrEmpty(_Url))
			{
				UrlController objUrls = new UrlController();
				string TrackingUrl = _Url;
				_Urltype = Common.Globals.GetURLType(_Url).ToString("g").Substring(0, 1);
				if (_Urltype == "U" && (_Url.StartsWith("~/images/")))
				{
					_Urltype = "I";
				}
				ViewState["UrlType"] = _Urltype;
				if (_Urltype == "F")
				{
					FileController objFiles = new FileController();
					if (_Url.ToLower().StartsWith("fileid="))
					{
						TrackingUrl = _Url;
						DotNetNuke.Services.FileSystem.FileInfo objFile = objFiles.GetFileById(int.Parse(_Url.Substring(7)), _objPortal.PortalID);
						if (objFile != null)
						{
							_Url = objFile.Folder + objFile.FileName;
						}
					}
					else
					{
						TrackingUrl = "FileID=" + objFiles.ConvertFilePathToFileId(_Url, _objPortal.PortalID).ToString();
					}
				}
				if (_Urltype == "M")
				{
					if (_Url.ToLower().StartsWith("userid="))
					{
						UserInfo objUser = UserController.GetUserById(_objPortal.PortalID, int.Parse(_Url.Substring(7)));
						if (objUser != null)
						{
							_Url = objUser.Username;
						}
					}
				}
				UrlTrackingInfo objUrlTracking = objUrls.GetUrlTracking(_objPortal.PortalID, TrackingUrl, ModuleID);
				if (objUrlTracking != null)
				{
					chkNewWindow.Checked = objUrlTracking.NewWindow;
					chkTrack.Checked = objUrlTracking.TrackClicks;
					chkLog.Checked = objUrlTracking.LogActivity;
				}
				else
				{
					chkNewWindow.Checked = false;
					chkTrack.Checked = false;
					chkLog.Checked = false;
				}
				ViewState["Url"] = _Url;
			}
			else
			{
				if (!String.IsNullOrEmpty(_Urltype))
				{
					optType.ClearSelection();
					if (optType.Items.FindByValue(_Urltype) != null)
					{
						optType.Items.FindByValue(_Urltype).Selected = true;
					}
					else
					{
						optType.Items[0].Selected = true;
					}
				}
				else
				{
					if (optType.Items.Count > 0)
					{
						optType.ClearSelection();
						optType.Items[0].Selected = true;
					}
				}
				chkNewWindow.Checked = false;
				chkTrack.Checked = false;
				chkLog.Checked = false;
			}
			_doRenderTypeControls = true;
		}
		private void DoRenderTypes()
		{
			string strCurrent = "";
			if (optType.SelectedIndex >= 0)
			{
				strCurrent = optType.SelectedItem.Value;
			}
			optType.Items.Clear();
			if (ShowNone)
			{
				if (optType.Items.FindByValue("N") == null)
				{
					optType.Items.Add(new ListItem(Localization.GetString("NoneType", LocalResourceFile), "N"));
				}
			}
			else
			{
				if (optType.Items.FindByValue("N") != null)
				{
					optType.Items.Remove(optType.Items.FindByValue("N"));
				}
			}
			if (ShowUrls)
			{
				if (optType.Items.FindByValue("U") == null)
				{
					optType.Items.Add(new ListItem(Localization.GetString("URLType", LocalResourceFile), "U"));
				}
			}
			else
			{
				if (optType.Items.FindByValue("U") != null)
				{
					optType.Items.Remove(optType.Items.FindByValue("U"));
				}
			}
			if (ShowTabs)
			{
				if (optType.Items.FindByValue("T") == null)
				{
					optType.Items.Add(new ListItem(Localization.GetString("TabType", LocalResourceFile), "T"));
				}
			}
			else
			{
				if (optType.Items.FindByValue("T") != null)
				{
					optType.Items.Remove(optType.Items.FindByValue("T"));
				}
			}
			if (ShowFiles)
			{
				if (optType.Items.FindByValue("F") == null)
				{
					optType.Items.Add(new ListItem(Localization.GetString("FileType", LocalResourceFile), "F"));
				}
			}
			else
			{
				if (optType.Items.FindByValue("F") != null)
				{
					optType.Items.Remove(optType.Items.FindByValue("F"));
				}
			}
			if (ShowImages)
			{
				if (optType.Items.FindByValue("I") == null)
				{
					optType.Items.Add(new ListItem(Localization.GetString("ImageType", LocalResourceFile), "I"));
				}
			}
			else
			{
				if (optType.Items.FindByValue("I") != null)
				{
					optType.Items.Remove(optType.Items.FindByValue("I"));
				}
			}
			if (ShowUsers)
			{
				if (optType.Items.FindByValue("M") == null)
				{
					optType.Items.Add(new ListItem(Localization.GetString("UserType", LocalResourceFile), "M"));
				}
			}
			else
			{
				if (optType.Items.FindByValue("M") != null)
				{
					optType.Items.Remove(optType.Items.FindByValue("M"));
				}
			}
			if (optType.Items.Count > 0)
			{
				if (!String.IsNullOrEmpty(strCurrent))
				{
					if (optType.Items.FindByValue(strCurrent) != null)
					{
						optType.Items.FindByValue(strCurrent).Selected = true;
					}
					else
					{
						optType.Items[0].Selected = true;
						_doRenderTypeControls = true;
					}
				}
				else
				{
					optType.Items[0].Selected = true;
					_doRenderTypeControls = true;
				}
				TypeRow.Visible = optType.Items.Count > 1;
			}
			else
			{
				TypeRow.Visible = false;
			}
		}
		private void DoCorrectRadioButtonList()
		{
			string _Urltype = Convert.ToString(ViewState["UrlType"]);
			if (optType.Items.Count > 0)
			{
				optType.ClearSelection();
				if (!String.IsNullOrEmpty(_Urltype))
				{
					if (optType.Items.FindByValue(_Urltype) != null)
					{
						optType.Items.FindByValue(_Urltype).Selected = true;
					}
					else
					{
						optType.Items[0].Selected = true;
						_Urltype = optType.Items[0].Value;
						ViewState["UrlType"] = _Urltype;
					}
				}
				else
				{
					optType.Items[0].Selected = true;
					_Urltype = optType.Items[0].Value;
					ViewState["UrlType"] = _Urltype;
				}
			}
		}
		private void DoRenderTypeControls()
		{
			string _Url = Convert.ToString(ViewState["Url"]);
			string _Urltype = Convert.ToString(ViewState["UrlType"]);
			UrlController objUrls = new UrlController();
			if (!String.IsNullOrEmpty(_Urltype))
			{
				switch (optType.SelectedItem.Value)
				{
					case "N":
						URLRow.Visible = false;
						TabRow.Visible = false;
						FileRow.Visible = false;
						UserRow.Visible = false;
						ImagesRow.Visible = false;
						break;
					case "I":
						URLRow.Visible = false;
						TabRow.Visible = false;
						FileRow.Visible = false;
						UserRow.Visible = false;
						ImagesRow.Visible = true;
						cboImages.Items.Clear();
						string strImagesFolder = Path.Combine(Common.Globals.ApplicationMapPath, "images\\");
						foreach (string strImage in Directory.GetFiles(strImagesFolder))
						{
							string img = strImage.Replace(strImagesFolder, "");
							cboImages.Items.Add(new ListItem(img, string.Format("~/images/{0}", img)));
						}

						if (cboImages.Items.FindByValue(_Url) != null)
						{
							cboImages.Items.FindByValue(_Url).Selected = true;
						}
						break;
					case "U":
						URLRow.Visible = true;
						TabRow.Visible = false;
						FileRow.Visible = false;
						UserRow.Visible = false;
						ImagesRow.Visible = false;
						if (String.IsNullOrEmpty(txtUrl.Text))
						{
							txtUrl.Text = _Url;
						}
						if (String.IsNullOrEmpty(txtUrl.Text))
						{
							txtUrl.Text = "http://";
						}
						txtUrl.Visible = true;
						cmdSelect.Visible = true;
						cboUrls.Visible = false;
						cmdAdd.Visible = false;
						cmdDelete.Visible = false;
						break;
					case "T":
						URLRow.Visible = false;
						TabRow.Visible = true;
						FileRow.Visible = false;
						UserRow.Visible = false;
						ImagesRow.Visible = false;
						cboTabs.Items.Clear();
						PortalSettings _settings = PortalController.GetCurrentPortalSettings();
						int excludeTabId = Null.NullInteger;
						if (!IncludeActiveTab)
						{
							excludeTabId = _settings.ActiveTab.TabID;
						}
                        cboTabs.DataSource = TabController.GetPortalTabs(_settings.PortalId, excludeTabId, !Required, "none available", true, false, false, true, false);
						cboTabs.DataBind();
						if (cboTabs.Items.FindByValue(_Url) != null)
						{
							cboTabs.Items.FindByValue(_Url).Selected = true;
						}
						break;
					case "F":
						URLRow.Visible = false;
						TabRow.Visible = false;
						FileRow.Visible = true;
						UserRow.Visible = false;
						ImagesRow.Visible = false;
						if (ViewState["FoldersLoaded"] == null || this._doReloadFolders)
						{
							LoadFolders("BROWSE,ADD");
							ViewState["FoldersLoaded"] = "Y";
						}
						if (cboFolders.Items.Count == 0)
						{
							lblMessage.Text = Localization.GetString("NoPermission", this.LocalResourceFile);
							FileRow.Visible = false;
							return;
						}
						string FileName = string.Empty;
						string FolderPath = string.Empty;
						string LastFileName = string.Empty;
						string LastFolderPath = string.Empty;
						bool _MustRedrawFiles = false;
						if (ViewState["LastFolderPath"] != null)
						{
							LastFolderPath = Convert.ToString(ViewState["LastFolderPath"]);
						}
						if (ViewState["LastFileName"] != null)
						{
							LastFileName = Convert.ToString(ViewState["LastFileName"]);
						}
						if (_Url != string.Empty)
						{
							FileName = _Url.Substring(_Url.LastIndexOf("/") + 1);
							FolderPath = _Url.Replace(FileName, "");
						}
						else
						{
							FileName = LastFileName;
							FolderPath = LastFolderPath;
						}
						if (cboFolders.Items.FindByValue(FolderPath) != null)
						{
							cboFolders.ClearSelection();
							cboFolders.Items.FindByValue(FolderPath).Selected = true;
						}
						else if (cboFolders.Items.Count > 0)
						{
							cboFolders.ClearSelection();
							cboFolders.Items[0].Selected = true;
							FolderPath = cboFolders.Items[0].Value;
						}
						if (ViewState["FilesLoaded"] == null || FolderPath != LastFolderPath || this._doReloadFiles)
						{
							_MustRedrawFiles = true;
							ViewState["FilesLoaded"] = "Y";
						}
						else
						{
							if (cboFiles.Items.Count > 0)
							{
								if ((Required && String.IsNullOrEmpty(cboFiles.Items[0].Value)) || (!Required && !String.IsNullOrEmpty(cboFiles.Items[0].Value)))
								{
									_MustRedrawFiles = true;
								}
							}
							else if (!Required)
							{
								_MustRedrawFiles = true;
							}
						}
						if (_MustRedrawFiles)
						{
							cboFiles.DataSource = GetFileList(!Required);
							cboFiles.DataBind();
							if (cboFiles.Items.FindByText(FileName) != null)
							{
								cboFiles.ClearSelection();
								cboFiles.Items.FindByText(FileName).Selected = true;
							}
						}
						cboFiles.Visible = true;
						txtFile.Visible = false;
						FolderController objFolders = new FolderController();
						FolderInfo objFolder = objFolders.GetFolder(_objPortal.PortalID, FolderPath, false);
						cmdUpload.Visible = ShowUpLoad && FolderPermissionController.CanAddFolder(objFolder);
						SetStorageLocationType();
						txtUrl.Visible = false;
						cmdSave.Visible = false;
						cmdCancel.Visible = false;
						if (cboFolders.SelectedIndex >= 0)
						{
							ViewState["LastFolderPath"] = cboFolders.SelectedValue;
						}
						else
						{
							ViewState["LastFolderPath"] = "";
						}
						if (cboFiles.SelectedIndex >= 0)
						{
							ViewState["LastFileName"] = cboFiles.SelectedValue;
						}
						else
						{
							ViewState["LastFileName"] = "";
						}
						break;
					case "M":
						URLRow.Visible = false;
						TabRow.Visible = false;
						FileRow.Visible = false;
						UserRow.Visible = true;
						ImagesRow.Visible = false;
						if (String.IsNullOrEmpty(txtUser.Text))
						{
							txtUser.Text = _Url;
						}
						break;
				}
			}
			else
			{
				URLRow.Visible = false;
				ImagesRow.Visible = false;
				TabRow.Visible = false;
				FileRow.Visible = false;
				UserRow.Visible = false;
			}
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			Framework.AJAX.RegisterPostBackControl(this.FindControl("cmdSave"));
			if (Request.IsAuthenticated == false)
			{
				this.Visible = false;
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cboFolders.SelectedIndexChanged += new EventHandler(cboFolders_SelectedIndexChanged);
			optType.SelectedIndexChanged += new EventHandler(optType_SelectedIndexChanged);
			cmdAdd.Click += new EventHandler(cmdAdd_Click);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdSave.Click += new EventHandler(cmdSave_Click);
			cmdSelect.Click += new EventHandler(cmdSelect_Click);
			cmdUpload.Click += new EventHandler(cmdUpload_Click);

			try
			{
				PortalController objPortals = new PortalController();
				if ((Request.QueryString["pid"] != null) && (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId || UserController.GetCurrentUserInfo().IsSuperUser))
				{
					_objPortal = objPortals.GetPortal(Int32.Parse(Request.QueryString["pid"]));
				}
				else
				{
					_objPortal = objPortals.GetPortal(PortalSettings.PortalId);
				}
				if (ViewState["IsUrlControlLoaded"] == null)
				{
					_doRenderTypes = true;
					_doChangeURL = true;
					ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			try
			{
				if (_doRenderTypes)
				{
					DoRenderTypes();
				}
				if (_doChangeURL)
				{
					DoChangeURL();
				}
				if (_doReloadFolders || _doReloadFiles)
				{
					DoCorrectRadioButtonList();
					_doRenderTypeControls = true;
				}
				if (_doRenderTypeControls)
				{
					if (!(_doReloadFolders || _doReloadFiles))
					{
						DoCorrectRadioButtonList();
					}
					DoRenderTypeControls();
				}
				ViewState["Url"] = null;
				ViewState["IsUrlControlLoaded"] = "Loaded";
			}
			catch (Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(new Exception("Error rendering URLControl subcontrols.", exc));
			}
		}

		protected void cboFolders_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			FolderController objFolders = new FolderController();
		    int PortalId = Null.NullInteger;

            if (!IsHostMenu || Request.QueryString["pid"] != null)
            {
                PortalId = _objPortal.PortalID;
            }
		    FolderInfo objFolder = objFolders.GetFolder(PortalId, cboFolders.SelectedValue, false);
			if (FolderPermissionController.CanAddFolder(objFolder))
			{
				if (!txtFile.Visible)
				{
					cmdSave.Visible = false;
					cmdUpload.Visible = ShowUpLoad;
				}
			}
			else
			{
				cboFiles.Visible = true;
				cmdUpload.Visible = false;
				txtFile.Visible = false;
				cmdSave.Visible = false;
				cmdCancel.Visible = false;
			}
			cboFiles.Items.Clear();
			cboFiles.DataSource = GetFileList(!Required);
			cboFiles.DataBind();
			SetStorageLocationType();
			if (cboFolders.SelectedIndex >= 0)
			{
				ViewState["LastFolderPath"] = cboFolders.SelectedValue;
			}
			else
			{
				ViewState["LastFolderPath"] = "";
			}
			if (cboFiles.SelectedIndex >= 0)
			{
				ViewState["LastFileName"] = cboFiles.SelectedValue;
			}
			else
			{
				ViewState["LastFileName"] = "";
			}
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void cmdAdd_Click(object sender, System.EventArgs e)
		{
			cboUrls.Visible = false;
			cmdSelect.Visible = true;
			txtUrl.Visible = true;
			cmdAdd.Visible = false;
			cmdDelete.Visible = false;
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
		{
			cboFiles.Visible = true;
			cmdUpload.Visible = true;
			txtFile.Visible = false;
			cmdSave.Visible = false;
			cmdCancel.Visible = false;
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void cmdDelete_Click(object sender, System.EventArgs e)
		{
			if (cboUrls.SelectedItem != null)
			{
				UrlController objUrls = new UrlController();
				objUrls.DeleteUrl(_objPortal.PortalID, cboUrls.SelectedItem.Value);
				LoadUrls();
			}
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void cmdSave_Click(System.Object sender, System.EventArgs e)
		{
			cmdUpload.Visible = false;
			if (String.IsNullOrEmpty(txtFile.PostedFile.FileName))
			{
				return;
			}
			string ParentFolderName;
			if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId)
			{
				ParentFolderName = Common.Globals.HostMapPath;
			}
			else
			{
				ParentFolderName = PortalSettings.HomeDirectoryMapPath;
			}
			ParentFolderName += cboFolders.SelectedItem.Value;
			string strExtension = Path.GetExtension(txtFile.PostedFile.FileName).Replace(".", "");
			if (!String.IsNullOrEmpty(FileFilter) && ("," + FileFilter.ToLower()).IndexOf("," + strExtension.ToLower()) == -1)
			{
				lblMessage.Text = string.Format(Localization.GetString("UploadError", this.LocalResourceFile), FileFilter, strExtension);
			}
			else
			{
				lblMessage.Text = FileSystemUtils.UploadFile(ParentFolderName.Replace("/", "\\"), txtFile.PostedFile, false);
			}
			if (lblMessage.Text == string.Empty)
			{
				cboFiles.Visible = true;
				cmdUpload.Visible = ShowUpLoad;
				txtFile.Visible = false;
				cmdSave.Visible = false;
				cmdCancel.Visible = false;
				DirectoryInfo Root = new DirectoryInfo(ParentFolderName);
				cboFiles.Items.Clear();
				cboFiles.DataSource = GetFileList(false);
				cboFiles.DataBind();
				string FileName = txtFile.PostedFile.FileName.Substring(txtFile.PostedFile.FileName.LastIndexOf("\\") + 1);
				if (cboFiles.Items.FindByText(FileName) != null)
				{
					cboFiles.Items.FindByText(FileName).Selected = true;
				}
				if (cboFiles.SelectedIndex >= 0)
				{
					ViewState["LastFileName"] = cboFiles.SelectedValue;
				}
				else
				{
					ViewState["LastFileName"] = "";
				}
			}
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void cmdSelect_Click(object sender, System.EventArgs e)
		{
			cboUrls.Visible = true;
			cmdSelect.Visible = false;
			txtUrl.Visible = false;
			cmdAdd.Visible = true;
			cmdDelete.Visible = PortalSecurity.IsInRole(_objPortal.AdministratorRoleName);
			LoadUrls();
			if (cboUrls.Items.FindByValue(txtUrl.Text) != null)
			{
				cboUrls.ClearSelection();
				cboUrls.Items.FindByValue(txtUrl.Text).Selected = true;
			}
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void cmdUpload_Click(object sender, System.EventArgs e)
		{
			string strSaveFolder = cboFolders.SelectedValue;
			LoadFolders("ADD");
			if (cboFolders.Items.FindByValue(strSaveFolder) != null)
			{
				cboFolders.Items.FindByValue(strSaveFolder).Selected = true;
				cboFiles.Visible = false;
				cmdUpload.Visible = false;
				txtFile.Visible = true;
				cmdSave.Visible = true;
				cmdCancel.Visible = true;
			}
			else
			{
				if (cboFolders.Items.Count > 0)
				{
					cboFolders.Items[0].Selected = true;
					cboFiles.Visible = false;
					cmdUpload.Visible = false;
					txtFile.Visible = true;
					cmdSave.Visible = true;
					cmdCancel.Visible = true;
				}
				else
				{
                    LoadFolders("BROWSE,ADD");
					cboFolders.Items.FindByValue(strSaveFolder).Selected = true;
					cboFiles.Visible = true;
					cmdUpload.Visible = false;
					txtFile.Visible = false;
					cmdSave.Visible = false;
					cmdCancel.Visible = false;
					lblMessage.Text = Localization.GetString("NoWritePermission", this.LocalResourceFile);
				}
			}
			_doRenderTypeControls = false;
			_doRenderTypes = false;
			_doChangeURL = false;
			_doReloadFolders = false;
			_doReloadFiles = false;
		}
		protected void optType_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			ViewState["UrlType"] = optType.SelectedItem.Value;
			_doRenderTypeControls = true;
		}
	}
}
